package org.gap.jseed;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;

class BuilderChainFactory<T> {
	private Builder<? extends T> builder;

	public BuilderChainFactory(Builder<? extends T> builder) {
		this.builder = builder;
	}

	public Builder<? extends T> createChain() {
		for (Annotation each : builder.getType().getAnnotations()) {
			createBuilderLink(each.annotationType());
		}
		
		return builder;
	}
	
	private void createBuilderLink(Class<? extends Annotation> annotationType) {
		try {
			Class<?> classBuilder = Class.forName(annotationType.getName() + "Builder");
			Constructor<?> constructor = classBuilder.getConstructor(Builder.class);
			@SuppressWarnings("unchecked") Builder<T> reflection = (Builder<T>)constructor.newInstance(builder);
			builder = reflection;
		} catch (Exception e) {
			e.printStackTrace(System.out);
//			throw new JSeedDefect("Unable to create BuilderLink: " + e);
		}
	}
}
